<?
/**
 *
 * Copyright (C) 2003-2011 Cory Powers
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 */
require_once('common.php');

////////////////////////////////////////////////////////////
// Code generated by CodeGen (http://uversaconsulting.net).
// Insert custom code where indicated below.
////////////////////////////////////////////////////////////

// SI_Attachment Class Definition 
////////////////////////////////////////////////////////////
class SI_Attachment{
	var $id, $project_id, $task_id, $activity_id, 
	  $path, $description;

	var $error;

	function SI_Attachment(){
		$this->error = '';
		$this->id = 0;
		$this->project_id = 0;
		$this->task_id = 0;
		$this->activity_id = 0;
		$this->path = '';
		$this->description = '';

	}

	function _populateData($values){
		if(is_array($values)){
			$this->id = $values[0];
			$this->project_id = $values[1];
			$this->task_id = $values[2];
			$this->activity_id = $values[3];
			$this->path = $values[4];
			$this->description = $values[5];
		}
	}

	function updateFromAssocArray($array){
		if(isset($array['id'])) $this->id = $array['id'];
		if(isset($array['project_id'])) $this->project_id = $array['project_id'];
		if(isset($array['task_id'])) $this->task_id = $array['task_id'];
		if(isset($array['activity_id'])) $this->activity_id = $array['activity_id'];
		if(isset($array['path'])) $this->path = $array['path'];
		if(isset($array['description'])) $this->description = $array['description'];
	}

	function escapeStrings(){
		global $db_conn;

		$vars = get_object_vars($this);
		foreach($vars as $key => $value){
			if(is_string($value)){
				$this->$key = $db_conn->escapeString($value);
			}
		}
	}

	function stripSlashes(){
		$vars = get_object_vars($this);
		foreach($vars as $key => $value){
			if(is_string($value)){
				$this->$key = stripcslashes($value);
			}
		}
	}

	function getLastError(){
		return $this->error;
	}

	function add(){
		global $db_conn;

		$this->escapeStrings();
		$result = $db_conn->query("INSERT INTO attachments (project_id, task_id, activity_id, path, ".
		  "description)".
		  " VALUES(".$this->project_id.", ".$this->task_id.", ".$this->activity_id.", '".$this->path."', ".
			"'".$this->description."')");
		$this->stripSlashes();
		if($result){
			$this->id = mysql_insert_id();
			return TRUE;
		}else{
			$this->error = "SI_Attachment::add() : ".$db_conn->getLastError()."\n";
			return FALSE;
		}
	}

	function update(){
		global $db_conn;

		if(!isset($this->id)){
			$this->error = "SI_Attachment::update() : Attachment id not set\n";
			return FALSE;
		}

		$this->escapeStrings();
		$result = $db_conn->query("UPDATE attachments SET project_id = ".$this->project_id.", ".
		  "task_id = ".$this->task_id.", activity_id = ".$this->activity_id.", ".
		  "path = '".$this->path."', description = '".$this->description."'".
		  " WHERE id = ".$this->id."");
		$this->stripSlashes();
		if($result){
			return TRUE;
		}else{
			$this->error = "SI_Attachment::update() : ".$db_conn->getLastError()."\n";
			return FALSE;
		}
	}

	function delete($id = NULL){
		global $db_conn;

		if(!isset($id)){
			$id = $this->id;
		}

		if(!isset($id)){
			$this->error = "SI_Attachment::delete() : Attachment id not set\n";
			return FALSE;
		}

		if(empty($this->path)){
			$this->get($id);
		}

		unlink($GLOBALS['CONFIG']['attachment_dir'].$this->path);
		$result = $db_conn->query("DELETE FROM attachments WHERE id = $id");

		if($result){
			return TRUE;
		}else{
			$this->error = "SI_Attachment::delete() : ".$db_conn->getLastError()."\n";
			return FALSE;
		}
	}

	function get($id = NULL){
		global $db_conn;

		if(!isset($id)){
			$id = $this->id;
		}

		if(!isset($id)){
			$this->error = "SI_Attachment::get() : Attachment id not set\n";
			return FALSE;
		}

		$Attachment = SI_Attachment::retrieveSet("WHERE id = $id", TRUE);
		if($Attachment === FALSE){
			return FALSE;
		}

		if(isset($Attachment[0])){
			$this->_populateData($Attachment[0]);
			$this->stripSlashes();
		}else{
			$this->error = "SI_Attachment::get() : No data retrieved from query\n";
			return FALSE;
		}
		return TRUE;
	}

	function retrieveSet($clause = '', $raw = FALSE){
		global $db_conn;

		if(!empty($clause)){
			$clause = trim($clause);
			if(strlen($clause) > 5){
				if(strtolower(substr($clause, 0, 5)) != "where" && strtolower(substr($clause, 0, 5)) != "order")
					$clause = "WHERE ".$clause;
			}else{
				$clause = "WHERE ".$clause;
			}
		}

		$result = $db_conn->query("SELECT  id, project_id, task_id, activity_id, path, description".
		  " FROM attachments ".$clause);

		if(!$result){
			$this->error = "SI_Attachment::retrieveSet(): ".$db_conn->getLastError()."\n";
			return FALSE;
		}

		$Attachment = array();
		while($row=$result->fetchRow()){
			if($raw == TRUE){
				$Attachment[] = $row;
			}else{
				$temp =& new SI_Attachment();
				$temp->_populateData($row);
				$Attachment[] =& $temp;
			}

		}

		return $Attachment;
	}
// BEGIN - Custom SI_Attachment methods 
////////////////////////////////////////////////////////////
	function getAttachmentsForProject($project_id){
		global $db_conn;

		$project_id = intval($project_id);
		$attachments = $this->retrieveSet("WHERE project_id = $project_id ");
		if($attachments === FALSE){
			return FALSE;
		}

		return $attachments;
	}

	function getAttachmentsForTask($task_id){
		global $db_conn;

		$task_id = intval($task_id);
		$attachments = $this->retrieveSet("WHERE task_id = $task_id ");
		if($attachments === FALSE){
			return FALSE;
		}

		return $attachments;
	}

	function getAttachmentsForActivity($act_id){
		global $db_conn;

		$act_id = intval($act_id);
		$attachments = $this->retrieveSet("WHERE activity_id = $act_id ");
		if($attachments === FALSE){
			return FALSE;
		}

		return $attachments;
	}

	function save($local_file, $filename){
		if(!is_readable($local_file)){
			$this->error = "SI_Attachment::save(): $local_file is not readable!";
			return FALSE;
		}

		$filename = $this->cleanFilename($filename);

		if(is_file($GLOBALS['CONFIG']['attachment_dir'].$filename)){
			$filename = $this->findFilename($filename);
		}

		if(copy($local_file, $GLOBALS['CONFIG']['attachment_dir'].$filename) === FALSE){
			$this->error = "SI_Attachment::save(): Error copying $local_file to ".$GLOBALS['CONFIG']['attachment_dir'].$filename."!";
			return FALSE;
		}

		return $filename;
	}

	function findFilename($filename){
		$out_filename = $filename;
		$pos = strrpos($filename, '.');
		if($pos > 0){
			$first = substr($filename, 0, $pos);
			$ext = substr($filename, $pos+1);
		}else{
			$first = $filename;
		}

		while(TRUE){
		 $rand = make_password(4);
			if(!is_file($GLOBALS['CONFIG']['attachment_dir'].$first.'-'.$rand.(empty($ext) ? '' : '.'.$ext))){
				$out_filename = $first.'-'.$rand.(empty($ext) ? '' : '.'.$ext);
				return $out_filename;
			}
		}
	}

	function cleanFilename($filename){
		return ereg_replace("[^a-zA-Z0-9._]","",preg_replace("#.*/#","",$filename));
	}

// END - Custom SI_Attachment methods
////////////////////////////////////////////////////////////
}

